package com.zhouxiaoge.transaction.jta.web;

import com.zhouxiaoge.transaction.jta.dao.CustomerRepository;
import com.zhouxiaoge.transaction.jta.domain.Customer;
import com.zhouxiaoge.transaction.jta.service.CustomerServiceTxInAnnotation;
import com.zhouxiaoge.transaction.jta.service.CustomerServiceTxInCode;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
@Slf4j
@RestController
@RequestMapping("/api/customer")
public class CustomerResource {

    private final CustomerServiceTxInAnnotation customerService;
    private final CustomerServiceTxInCode customerServiceInCode;
    private final CustomerRepository customerRepository;

    public CustomerResource(CustomerServiceTxInAnnotation customerService, CustomerServiceTxInCode customerServiceInCode, CustomerRepository customerRepository) {
        this.customerService = customerService;
        this.customerServiceInCode = customerServiceInCode;
        this.customerRepository = customerRepository;
    }

    @PostMapping("/annotation")
    public Customer createInAnnotation(@RequestBody Customer customer) {
        log.info("CustomerResource create in annotation create customer:{}", customer.getUsername());
        return customerService.create(customer);
    }

    @PostMapping("/code")
    public Customer createInCode(@RequestBody Customer customer) {
        log.info("CustomerResource create in code create customer:{}", customer.getUsername());
        return customerServiceInCode.create(customer);
    }

    @GetMapping("")
    public List<Customer> getAll() {
        return customerRepository.findAll();
    }

}
